package com.powernode.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.powernode.domain.Member;
import com.powernode.model.Result;
import com.powernode.service.MemberService;
import com.powernode.utils.AuthUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 微信小程序会员业务管理控制层
 * 购物系统：会员控制层
 */
@Api(tags = "购物系统会员接口管理")
@RequestMapping("p/user")
@RestController
public class MemberController {

    @Autowired
    private MemberService memberService;

    /**
     * 更新会员的头像和昵称
     *
     * @param member 会员对象
     * @return
     */

    @ApiOperation("更新会员的头像和昵称")
    @PutMapping("setUserInfo")
    public Result<String> modifyMemberInfo(@RequestBody Member member) {
        Boolean modified = memberService.modifyMemberInfoByOpenId(member);
        return Result.handle(modified);
    }

    /**
     * 查询会员是否绑定手机号码
     * <p>
     * 本方法用于检查当前会员是否已经绑定手机号码通过查询会员的OpenID来确定手机号是否已绑定
     *
     * @return 返回一个Result对象，其中包含一个布尔值，表示会员是否绑定手机号码
     */
    @ApiOperation("查询会员是否绑定手机号码")
    @GetMapping("isBindPhone")
    public Result<Boolean> loadMemberIsBindPhone() {
        // 获取当前会员的OpenID
        String openId = AuthUtils.getMemberOpenId();
        // 根据OpenID查询会员信息
        Member member = memberService.getOne(new LambdaQueryWrapper<Member>().eq(Member::getOpenId, openId));
        // 判断会员的手机号是否为空，并返回结果
        return Result.success(StringUtils.hasText(member.getUserMobile()));
    }

    //////////////////////// feign接口 ////////////////////////
    @GetMapping("getMemberListByOpenIds")
    public Result<List<Member>> getMemberListByOpenIds(@RequestParam List<String> openIds) {
        List<Member> list = memberService.lambdaQuery().in(Member::getOpenId, openIds).list();
        return Result.success(list);
    }


}
